<?php

namespace App\Http\Requests\Admin\GameTool;

use Illuminate\Foundation\Http\FormRequest;

class RedPacketIndexRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'start_time' => 'before_or_equal:end_time',
            'end_time' => 'after_or_equal:start_date',
        ];
    }

    public function messages()
    {
        return [
            'start_time.before_or_equal' => '开始时间必须小于或等于结束时间',
            'end_time.after_or_equal' => '结束时间必须大于或等于开始时间',
        ];
    }

    protected function prepareForValidation()
    {
        $reqStartTime = isset($this->start_time) ? $this->start_time : date('Y-m-d', strtotime('-7day'));
        $reqEndTime = isset($this->end_time) ? $this->end_time : date('Y-m-d');
        $reqPageSize = isset($this->page_size) ? intval($this->page_size) : 10;
        $reqOrderBy = isset($this->order_by) ? $this->order_by : 'stat';
        $reqOrderDirection = isset($this->order_direction) ? $this->order_direction : 'ASC';


        $this->merge([
            'start_time' => $reqStartTime,
            'end_time' => $reqEndTime,
            'page_size' => $reqPageSize,
            'order_by' => $reqOrderBy,
            'order_direction' => $reqOrderDirection,
        ]);
    }
}
